package cn.itcast.com.network.tcp.exercise04;

import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author zhangshuai@Cskaoyan.onaliyun.com.
 * @version 1.0
 */
public class Server {

  public static void main(String[] args) throws IOException {
    // 1. 创建ServerSocket
    ServerSocket serverSocket = new ServerSocket(9999);

    Socket socket = serverSocket.accept();

    InputStream in = socket.getInputStream();
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    char[] buf = new char[1024];
    int len;

    FileWriter fileWriter = new FileWriter("D:\\代码\\代码\\代码\\cn\\itcast\\com\\network\\tcp\\exer04\\upload.txt");
    while ((len = br.read(buf)) != -1) {
      fileWriter.write(buf, 0, len);
      fileWriter.flush();
    }


    // 给客户端发送反馈消息
    OutputStream out = socket.getOutputStream();
    out.write("文件上传完毕!".getBytes());


    // 释放资源
    fileWriter.close();
    socket.close();
    serverSocket.close();

  }

}
